﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace TRAC_Demo
{
    // T.R.A.C.
    // Main Author: Ryan Redburn
    // 10/25/2013

    #region [ Updates]

    // Updated: 12/2/2013
    // By: Ryan Redburn
    // Updates: Minor functionality changes

    #endregion

    public partial class FormAdminManagement : Form
    {
        #region [ Constructors ]

        public FormAdminManagement()
        {
            InitializeComponent();
        }

        #endregion

        #region [ Events ]

        // When the form loads the datagridview is popluated and the account text box is given focus
        private void FormAdminManagement_Load(object sender, EventArgs e)
        {
            CompileTable();
            txtAccount.Focus();
        }

        // Changes the color of the create form to provide an example to the user
        private void cmbColor_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selection = (string)cmbColor.SelectedItem;
            List<int> colorValues = ColorKeeper.RetrieveColor(selection);
            this.BackColor = Color.FromArgb(colorValues[0], colorValues[1], colorValues[2], colorValues[3]);
        }

        // Alters form functionality based on radio button selection
        private void radAll_CheckedChanged(object sender, EventArgs e)
        {
            if (radPassword.Checked)
            {
                cmbColor.Enabled = false;
                cmbPrivileges.Enabled = false;
                txtPassword.Enabled = true;
                cmbColor.SelectedIndex = -1;
                cmbPrivileges.SelectedIndex = -1;
                txtAccount.Text = "";
                txtPassword.Text = "";
            }
            else if (radColor.Checked)
            {
                cmbColor.Enabled = true;
                cmbPrivileges.Enabled = false;
                txtPassword.Enabled = false;
                cmbColor.SelectedIndex = -1;
                cmbPrivileges.SelectedIndex = -1;
                txtAccount.Text = "";
                txtPassword.Text = "";
            }
            else if (radPrivileges.Checked)
            {
                cmbColor.Enabled = false;
                cmbPrivileges.Enabled = true;
                txtPassword.Enabled = false;
                cmbColor.SelectedIndex = -1;
                cmbPrivileges.SelectedIndex = -1;
                txtAccount.Text = "";
                txtPassword.Text = "";
            }
            else if (radDelete.Checked)
            {
                cmbColor.Enabled = false;
                cmbPrivileges.Enabled = false;
                txtPassword.Enabled = false;
                cmbColor.SelectedIndex = -1;
                cmbPrivileges.SelectedIndex = -1;
                txtAccount.Text = "";
                txtPassword.Text = "";
            }
        }

        // Executes the selected command and gives focus back to the account text box
        private void btnExecute_Click(object sender, EventArgs e)
        {
            try
            {
                ProcessCommand();
                txtAccount.Focus();
                txtAccount.SelectAll();
            }
            catch (XmlException)
            {
                MessageBox.Show("The given user account does not exist.", "Invalid Account");
            }
        }

        // Closes the management form
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region [ Methods ]

        // Creates the datatable adding all agent information
        private void CompileTable()
        {
            DataTable Accounts = new DataTable();
            Accounts.Columns.Add("FirstName", typeof(string));
            Accounts.Columns.Add("LastName", typeof(string));
            Accounts.Columns.Add("EmployeeID", typeof(string));
            Accounts.Columns.Add("UserID", typeof(string));
            Accounts.Columns.Add("Password", typeof(string));
            Accounts.Columns.Add("AccountType", typeof(string));
            Accounts.Columns.Add("UserColor", typeof(string));

            List<int> empIDs = AccountKeeper.GetAllEmpIDs();
            int rowNmbr = empIDs.Count;

            // Adds user rows to the table
            for (int i = 0; i < rowNmbr; i++)
            {
                string[] acctInfo = CompileUserInfo(empIDs[i]);
                Accounts.Rows.Add(acctInfo[0], acctInfo[1], acctInfo[2], acctInfo[3], acctInfo[4], acctInfo[5], acctInfo[6]);
            }

            dgvAccounts.DataSource = Accounts;
        }

        // Performs the necessary steps in order to execute the selected command
        private void ProcessCommand()
        {
            if (ValidatePopulace())
            {
                ExecuteCommand(txtAccount.Text);
                CompileTable();
            }
            else
            {
                MessageBox.Show("One or more fields required for the selected command have been left empty.", "Empty Required Fields");
            }
        }

        // Executes the currently selected command
        private void ExecuteCommand(string userID)
        {
            if (radPassword.Checked)
            {
                AccountKeeper.ChangePassword(userID, txtPassword.Text);
            }
            else if (radColor.Checked)
            {
                AccountKeeper.ChangeUserColor(userID, (string)cmbColor.SelectedItem);
            }
            else if (radPrivileges.Checked)
            {
                AccountKeeper.ChangeAcctType(userID, (string)cmbPrivileges.SelectedItem);
            }
            else if (radDelete.Checked)
            {
                AccountKeeper.DeleteUser(userID);
            }
        }

        #endregion

        #region [ Helper Methods ]

        // Determines the number of employees in order to decide the number of rows needed in the table
        private int GetRowNumber()
        {
            TRACobjectsDataContext TRAC_DB = new TRACobjectsDataContext();

            int nmbrOfAccts;

            var agents = from records in TRAC_DB.Agents
                         select records;

            nmbrOfAccts = agents.Count();

            TRAC_DB.Dispose();

            return nmbrOfAccts;
        }

        // Complies agent information for use in the datatable
        private string[] CompileUserInfo(int empID)
        {
            TRACobjectsDataContext TRAC_DB = new TRACobjectsDataContext();

            List<string> userInfo = AccountKeeper.GetAcctInfo(empID);

            string[] rowData = new string[7];

            var results = from agents in TRAC_DB.Agents
                          where agents.EmployeeID == empID
                          select agents;

            if (userInfo.Count != 5)
            {
                // If no user data is found the blanks are filled in
                foreach (var result in results)
                {
                    rowData[0] = result.fName.Trim(); // First name
                    rowData[1] = result.lName.Trim(); // Last name
                    rowData[2] = result.EmployeeID.ToString(); // Employee id
                    rowData[3] = "NA";
                    rowData[4] = "NA";
                    rowData[5] = "NA";
                    rowData[6] = "NA";
                }
            }
            else
            {
                // Prepares user data for use in the datatable
                foreach (var result in results)
                {
                    rowData[0] = result.fName.Trim(); // First name
                    rowData[1] = result.lName.Trim(); // Last name
                    rowData[2] = result.EmployeeID.ToString(); // Employee id
                    rowData[3] = userInfo[0]; // User id
                    rowData[4] = userInfo[2]; // Password
                    rowData[5] = userInfo[3]; // Account type
                    rowData[6] = userInfo[4]; // User color
                }
            }

            TRAC_DB.Dispose();

            return rowData;
        }

        // Confirms that the required fields for the selected command are populated
        private bool ValidatePopulace()
        {
            if (radPassword.Checked)
            {
                if (txtAccount.Text != "" && txtPassword.Text != "")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (radColor.Checked)
            {
                if (txtAccount.Text != "" && (string)cmbColor.SelectedItem != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (radPrivileges.Checked)
            {
                if (txtAccount.Text != "" && (string)cmbPrivileges.SelectedItem != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (radDelete.Checked)
            {
                if (txtAccount.Text != "")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        #endregion
    }
}
